<template>
    <!-- 批量绑定子设备对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1500px" append-to-body
    >
      <el-form label-width="80px">

        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="170px">

          <el-form-item label="设备ID" prop="id">
            <el-input
              v-model="queryParams.id"
              placeholder="请输入设备ID"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备名称" prop="deviceName">
            <el-input
              v-model="queryParams.deviceName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="所属产品" prop="productId">
            <el-select v-model="queryParams.productId" placeholder="请选择所属产品" clearable>
              <el-option
                v-for="dict in productList"
                :key="dict.id"
                :label="dict.productName"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading"  ref="deviceTable" :data="childrenDeviceList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="设备ID" align="center" prop="id"/>
          <el-table-column label="设备名称" align="center" prop="deviceName"/>
          <el-table-column prop="productId" label="产品名称" header-align="center" align="center">
            <template slot-scope="scope">
            <span v-for="(item, index) in productList" :key="index" v-if="scope.row.productId === item.id">
              {{ item.productName }}
            </span>
            </template>
          </el-table-column>
          <el-table-column prop="deviceType" label="设备类型" header-align="center" align="center">
            <template slot-scope="scope">
            <span v-for="(item, index) in productList" :key="index" v-if="scope.row.productId === item.id">
            <dict-tag :options="dict.type.iot_product_device_type" :value="item.deviceType"/>
            </span>
            </template>
          </el-table-column>
          <el-table-column label="注册日期" sortable align="center" prop="registryTime"/>
          <el-table-column label="状态" align="center" prop="deviceState">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.iot_device_state" :value="scope.row.deviceState"/>
            </template>
          </el-table-column>
          <el-table-column label="描述" align="center" prop="description"/>
        </el-table>

        <!-- 表格分页 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
  </el-dialog>
</template>

<script>
  import { bindChildrenDevice, listChildrenDevice } from '@/api/iot/device'
  import {mapState} from 'vuex'
  import {queryProduct} from '@/api/iot/product'

  export default {
    name: "BindChildrenDevice",
    // 字典
    dicts: [
      'iot_device_state',
      //设备类型
      'iot_product_device_type'
    ],
    data() {
      return {
        //状态统计
        count: {},
        // 设备表格数据
        childrenDeviceList: [],
        //产品列表
        productList: [],
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          id: null,
          deviceName: null,
          productId: null,
        },
      }
    },
    computed: {
      ...mapState({
        deviceId: state => state.device.deviceId
      }),
    },
    methods: {
      getList() {
        this.loading = true;
        this.queryParams.deviceType = 'childrenDevice';
        queryProduct(this.queryParams).then(response => {
          this.productList = response.data
        });
        listChildrenDevice(this.queryParams).then(response => {
          this.total = response.total;
          this.childrenDeviceList = response.rows;
        });
        this.loading = false
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        selection.forEach(row => {
          if (row.publishState === 1) {
            this.$refs.deviceTable.toggleRowSelection(row)
          }
        });
        this.ids = selection.map(item => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      // 搜索按钮操作
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList()
      },
      // 重置按钮操作
      resetQuery() {
        this.resetForm('queryForm');
        this.handleQuery()
      },
      // 取消按钮
      cancel() {
        this.open = false;
      },
      /** 绑定按钮操作 */
      handleUpdate(row) {
        this.visible = true;
        this.getList();
        this.open = true;
      },
      /** 提交按钮 */
      submitForm() {
        const ids = this.ids;
        bindChildrenDevice(ids, this.deviceId).then(({ code }) => {
           if (code === 200) {
             this.getList();
             this.$modal.msgSuccess('绑定成功');
             this.open = false;
             this.$emit('update')
           }
         })
      },
    }
  };
</script>

<style lang="scss" scoped>

</style>
